//
//  LiveNewFollowBubbleView.m
//  LottieAnimation
//
//  Created by tianlong on 2020/11/30.
//  Copyright © 2020 tianlong. All rights reserved.
//

#import "LiveNewFollowBubbleView.h"
#import "Masonry.h"

@interface LiveNewFollowBubbleView ()
@property (weak,   nonatomic) UIImageView *iconView;
@property (weak,   nonatomic) UIImageView *bubbleView;
@property (weak,   nonatomic) UIImageView *bubLefView;//小尖头
@property (weak,   nonatomic) UIImageView *bubRigView;//矩形框
@property (weak,   nonatomic) UILabel *textLabel;
@end

@implementation LiveNewFollowBubbleView

static NSInteger privateColor = 0xFEFBE5;
static CGFloat TeacherIconW = 40.0f;
static CGFloat TextLabelLef = 16.0f;
static CGFloat TextLabelRig = 17.0f;
static CGFloat BubHeight= 36.0f;
static CGFloat BubLef   = 4.0f;
static CGFloat PrivateAnimationDuration = 0.30f;

- (instancetype)initWithEntity:(id)entity {
    self = [super init];
    if (self) {
        
        UIImageView *bgView = [[UIImageView alloc] init];
        [self addSubview:bgView];
        self.bubbleView = bgView;
        
        UIImageView *bubLefView = [[UIImageView alloc] init];
        bubLefView.image = [UIImage imageNamed:@"livenew_privatechat_bubble_lef"];
        [self.bubbleView addSubview:bubLefView];
        self.bubLefView = bubLefView;
        
        UIImage *bgImg = [UIImage imageNamed:@"livenew_privatechat_bubble_bg"];
        UIImageView *bubRigView = [[UIImageView alloc] init];
        bubRigView.image = [bgImg wx_stretchImageFromCenter];
        [self.bubbleView addSubview:bubRigView];
        self.bubRigView = bubRigView;
        
        UILabel *textLabel = [[UILabel alloc] init];
        textLabel.text = @"哈哈哈哈哈";
        textLabel.alpha = 0.0f;
        textLabel.font = [UIFont fontPingFangSCWithSize:15 *self.bigClassScale];
        textLabel.textColor = [UIColor colorWithHexValue:0xD16B28];
        [self addSubview:textLabel];
        self.textLabel = textLabel;
        
        UIImageView *iconView = [[UIImageView alloc] init];
        iconView.backgroundColor = [UIColor wx_colorWithHEXValue:privateColor alpha:1.0f];
        iconView.transform = CGAffineTransformMakeScale(0, 0);
        [self addSubview:iconView];
        self.iconView = iconView;
        
        CGPoint offset = CGPointMake(0, 2);
        [self setupShadowWithView:self.bubbleView opacity:0.2f shadowOffset:offset shadowRadius:2];
        [self setupShadowWithView:self.iconView opacity:0.2f shadowOffset:offset shadowRadius:2];
        
        [self setupLayout];
        self.layer.masksToBounds = NO;

    }
    return self;
}

- (void)setupLayout {
    
    __weak typeof(self)selfWeak = self;
    CGFloat scale = self.bigClassScale;
    
    CGFloat iconW = [self iconW];
    self.iconView.layer.cornerRadius = iconW *0.5;
    [self.iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(selfWeak);
        make.width.height.offset(iconW);
        make.left.offset(0);
    }];
    
    [self.textLabel sizeToFit];
    [self layoutBubbleViewWidthIsZero:YES];
    
    CGFloat labelLef = TextLabelLef *scale;
    CGFloat labelW = self.textLabel.width;
    CGFloat labelH = self.textLabel.height;
    [self.textLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(selfWeak.bubbleView.mas_left).offset(labelLef);
        make.centerY.equalTo(selfWeak);
        make.width.offset(labelW);
        make.height.offset(labelH);
    }];
}

- (void)layoutBubbleViewWidthIsZero:(BOOL)isZero {
    __weak typeof(self)selfWeak = self;
    CGFloat scale   = self.bigClassScale;
    CGFloat zeroW = 0.50f;
    CGFloat corerW  = isZero?zeroW:(6 *scale);
    CGFloat cornerH  = 12 *scale;
    CGFloat bubW = isZero?0:(self.textLabel.width + TextLabelLef *scale +TextLabelRig *scale);
    CGFloat bubRigW = isZero?zeroW:(bubW - corerW);
    CGFloat bubH = BubHeight *scale;
    [self.bubbleView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(bubH);
        make.centerY.equalTo(selfWeak);
        make.width.offset(bubW);
        make.left.equalTo(selfWeak.iconView.mas_right).offset(BubLef *scale);
    }];
    [self.bubLefView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.centerY.equalTo(selfWeak.bubbleView);
        make.width.offset(corerW);
        make.height.offset(cornerH);
    }];
    [self.bubRigView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(corerW);
        make.centerY.equalTo(selfWeak.bubbleView);
        make.width.offset(bubRigW);
        make.height.offset(bubH);
    }];
}


//老师头像宽度
- (CGFloat)iconW {
    return TeacherIconW *self.bigClassScale;
}

//设置阴影
- (void)setupShadowWithView:(UIView *)view
                    opacity:(CGFloat)opacity
               shadowOffset:(CGPoint)shadowOffset
               shadowRadius:(CGFloat)shadowRadius {
    view.layer.shadowOpacity = opacity;
    view.layer.shadowColor = [UIColor colorWithHexValue:0x000000].CGColor;
    view.layer.shadowOffset = CGSizeMake(shadowOffset.x, shadowOffset.y);
    view.layer.shadowRadius = shadowRadius;
}

//播放头像动效
- (void)playIconAnimate:(void (^)(BOOL finished))completion isShow:(BOOL)isShow {
    
    CGFloat scale = 0.00001;
    [UIView animateWithDuration:PrivateAnimationDuration delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:1 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        if (isShow) {
            self.iconView.transform = CGAffineTransformIdentity;
        } else {
            self.iconView.transform = CGAffineTransformMakeScale(scale, scale);
        }
    } completion:^(BOOL finished) {
        if (completion) {
            completion(finished);
        }
    }];
}

//播放背景气泡动效
- (void)playBubbleViewAnimate:(void (^)(BOOL finished))completion isShow:(BOOL)isShow {
    __weak typeof(self)selfWeak = self;
    [self layoutBubbleViewWidthIsZero:!isShow];
    if (isShow) {
        self.bubbleView.hidden = NO;
    }
    [UIView animateWithDuration:PrivateAnimationDuration animations:^{
        [selfWeak layoutIfNeeded];
    } completion:^(BOOL finished) {
        if (!isShow) {
            selfWeak.bubbleView.hidden = YES;
        }
        if (completion) {
            completion(finished);
        }
    }];
}

//播放文本透明度动效
- (void)playTextLabelViewAnimate:(void (^)(BOOL finished))completion isShow:(BOOL)isShow {
    __weak typeof(self)selfWeak = self;
    [UIView animateWithDuration:PrivateAnimationDuration animations:^{
        selfWeak.textLabel.alpha = isShow?1:0;
    } completion:^(BOOL finished) {
        if (completion) {
            completion(finished);
        }
    }];
}


#pragma mark - public function

- (CGSize)viewSize {
    [self layoutIfNeeded];
    CGFloat h = [self iconW];
    CGFloat w = CGRectGetMaxX(self.bubbleView.frame);
    return CGSizeMake(w, h);
}

- (void)showWithAnimate:(BOOL)animate {
    if (animate) {
        __weak typeof(self)selfWeak = self;
        BOOL isShow = YES;
        [self playIconAnimate:^(BOOL finished) {
            if (finished) {
                [selfWeak playBubbleViewAnimate:^(BOOL isFinished) {
                    [selfWeak playTextLabelViewAnimate:nil isShow:isShow];
                } isShow:isShow];
            }
        } isShow:isShow];
    } else {
        self.iconView.transform = CGAffineTransformIdentity;
    }
}

- (void)dismissWithAimate:(BOOL)animate {
    __weak typeof(self)selfWeak = self;
    if (animate) {
        BOOL isShow = NO;
        [self playTextLabelViewAnimate:^(BOOL finished) {
            [selfWeak playBubbleViewAnimate:^(BOOL isFinished) {
                [selfWeak playIconAnimate:^(BOOL finished) {
                    
                } isShow:isShow];
            } isShow:isShow];
        } isShow:isShow];
    } else {
        self.iconView.transform = CGAffineTransformMakeScale(0, 0);
        self.textLabel.alpha = 0.0f;
    }
}

- (void)dealloc {
    [self.layer removeAllAnimations];
}

@end
